<?php

namespace App\Http\Controllers\SystemManagement;

use App\Http\Controllers\IndexController;
use App\Models\Constant\Fields;
use Illuminate\Http\Request;
use App\Models\SystemManagement\RoleCategory;

/**
 * @group 系统管理->角色类型管理
 *
 * 采用 restful api
 *
 * Body Parameters 表示请求的所需要的参数
 *
 * Query Parameters 表示返回参数的名称
 */

class RoleCategoryController extends IndexController
{
    /**
     * 添加角色类型数据
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam id int required ID
     * @bodyParam name 类型名称
     *
     * @response 201 {"code":201,"msg":"created","data":"新增成功"}
     * @response 400 {"code":400,"msg":"param error","data":"请添加数据/新增失败"}
     */
    public function createRoleCategory(\App\Http\Requests\System\RoleCategory $request){
        return $this->create($request,new RoleCategory());
    }

    /**
     * 角色类型列表-带分页
     * @return false|string
     *
     * @queryParam id 用户ID
     * @queryParam name 类型名称
     *
     * @response 200 {code":200,"msg":"success","data":"成功"}
     * @response 404 {"code":404,"msg":"not found","data":"没有查询到任何数据"}
     */
    public function getRoleCategory(){
        $per = \request()->input('per_page');
        return $this->getList(new RoleCategory(),$per);
    }

    /**
     * 角色类型列表-不带分页
     * @return \Illuminate\Http\JsonResponse
     *
     * @queryParam id 用户ID
     * @queryParam name 类型名称
     *
     * @response 200 {code":200,"msg":"success","data":"成功"}
     * @response 404 {"code":404,"msg":"not found","data":"没有查询到任何数据"}
     */
    public function getAll(){
        return response()->json(
            encodeMsg(RoleCategory::get(),200)
        );
    }

    /**
     * 查看角色类型
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     *
     * @queryParam id 用户ID
     * @queryParam name 类型名称
     *
     * @response 200 {code":200,"msg":"success","data":"成功"}
     * @response 404 {"code":404,"msg":"not found","data":"该用户不存在"}
     */
    public function getOneRoleCategory($id){
        return $this->show($id,new RoleCategory());
    }

    /**
     * 修改角色类型
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam id int required ID
     * @bodyParam name 类型名称
     *
     * @response 205 {code":205,"msg":"updated","data":"成功"}
     * @response 400 {"code":400,"msg":"parameter error","data":"失败"}
     * @response 404 {"code":404,"msg":"not found","data":"该用户不存在"}
     */
    public function updateRoleCategory(\App\Http\Requests\System\RoleCategory $request,$id){
        return $this->update($request,$id,new RoleCategory());
    }

    /**
     * 删除角色类型
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam ids array required id数组
     *
     * @response 204 {code":204,"msg":"deleted","data":"成功"}
     * @response 400 {"code":400,"msg":"parameter error","data":"失败"}
     */
    public function del(Request $request){
        $ids = $request->input('ids');
        foreach ($ids as $k=>$id){
            $ids[$k] = (int) $id;
        }
        try{
            RoleCategory::whereIn(Fields::ID,$ids)->delete();
            return response()->json(encodeMsg("删除成功",204));
        }catch (\Exception $e){
            //dd($e->getMessage());
            return response()->json(encodeMsg("删除失败",400));
        }
    }

    /**
     * 恢复数据
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam ids array required id数组
     *
     * @response 205 {code":205,"msg":"updated","data":"成功"}
     * @response 400 {"code":400,"msg":"parameter error","data":"失败"}
     */
    public function rec(Request $request){
        return $this->recover($request,new RoleCategory());
    }

    /**
     * 按名称搜索
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam name string required 名称
     *
     * @queryParam id 用户ID
     * @queryParam name 类型名称
     *
     * @response 200 {code":200,"msg":"success","data":"成功"}
     * @response 400 {"code":400,"msg":"parameter error","data":"失败"}
     * @response 404 {"code":404,"msg":"not found","data":"没有查询到任何数据"}
     */
    public function getBy(Request $request){
        return $this->getByCond($request,new RoleCategory());
    }
}
